package net.diaowen.common.utils.pdf;


import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.io.util.StreamUtil;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.draw.DashedLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.LineSeparator;
import com.itextpdf.layout.element.Paragraph;
import net.diaowen.TestPDF;
import com.itextpdf.kernel.colors.Color;

import java.io.*;


public class PDFExportUtil {
    private String path;
    private String fileName;
    private PdfDocument pdfDocument;
    private Document document;
    private int titleIndex = 0;

    public PDFExportUtil(String path, String fileName) throws IOException {
        this.path = path;
        this.fileName = fileName;
        File file = new File(this.path);
//        if (!file.exists())
//            file.mkdirs();
        this.pdfDocument = new PdfDocument(new PdfWriter(this.path + this.fileName));
        this.document = new Document(pdfDocument, PageSize.A4);
        this.document.setMargins(20, 20, 20, 20);
        this.initFont();

    }

    private void initFont() throws IOException {
        InputStream inputStream = TestPDF.class.getResourceAsStream("/FangZhengShuSongJianTi-1.ttf");
        byte[] data;
        if (inputStream != null) {
            data = StreamUtil.inputStreamToArray(inputStream);
        } else {
            throw new IOException("字体文件不存在");
        }
        FontProgram fontPro = FontProgramFactory.createFont(data, true);

        PdfFont font = PdfFontFactory.createFont(fontPro);
        this.pdfDocument.addFont(font);
        this.document.setFont(font);
    }

    public void saveAndClose() {
        pdfDocument.close();
    }

    public void addItem() {
        LineSeparator dashedLine = new LineSeparator(new DashedLine(2));
        this.document.add(dashedLine);
        this.titleIndex = 0;
    }

    public void addTitle(String title) throws IOException {
        this.addText(String.format("%d、%s", this.titleIndex + 1, title), 15, 0);
        this.titleIndex++;
    }

    public void addContent(String content) throws IOException {
        this.addText(content, 10, 10);
    }

    public void addText(String text, float fontSize, float marginLeft) {
        if (text != null && !text.equals("")) {
//            this.document.add(new Paragraph(text).setMarginLeft(marginLeft).setFontSize(fontSize).setStrokeColor(
//                    new DeviceRgb(0.137254f, 0.443137f, 0.941176f)).setStrokeColor(new Color(0,0,255)));
            this.document.add(new Paragraph(text).setMarginLeft(marginLeft).setFontSize(fontSize).setStrokeColor(
                    new DeviceRgb(1, 0, 0))
            );
        } else {
            this.document.add(new Paragraph("<无内容>"));
        }
    }

    public void addImage(String imagePath, String fileName) throws IOException {
        try {
            this.document.add(new Image(ImageDataFactory.create(imagePath)));
        } catch (com.itextpdf.io.exceptions.IOException e) {
            this.addContent("<文件：" + fileName + ">");
        }
    }
}
